package com.zdzx.controller.admin.base;

import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdzx.base.common.util.RedisUtil;
import com.zdzx.base.common.util.UserInfoUtil;
import com.zdzx.common.result.PageInfo;
import com.zdzx.common.result.PageUtil;
import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.TenantFunctionMapper;
import com.zdzx.mapper.TenantMapper;
import com.zdzx.model.service.base.param.EditTenant;
import com.zdzx.model.service.base.result.UserInfo;
import com.zdzx.model.service.base.vo.Tenant;
import com.zdzx.model.service.base.vo.TenantFunction;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "租户")
@RestController
@RequestMapping("/admin")
public class TenantAdminController {

	@Autowired
	private TenantFunctionMapper tenantFunctionMapper;
	@Autowired
	private TenantMapper tenantMapper;
	@Autowired
	private RedisUtil redisUtil;
	@ApiOperation(value = "获取列表", notes = "")
	@GetMapping("/tenant")
	public Result<PageInfo<Tenant>> list(@ApiParam("") @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false) Integer pageSize,
			@ApiParam("") @RequestParam(value = "tenantName", required = false) String tenantName){
		Page<Tenant> page=new Page<Tenant>(pageNum,pageSize);
		QueryWrapper<Tenant> queryWrapper=new QueryWrapper<Tenant>();
		if(!StringUtils.isEmpty(tenantName))
		queryWrapper.eq("tenant_name",tenantName);
		IPage<Tenant> info = tenantMapper.selectPage(page,queryWrapper);
		PageInfo<Tenant> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(), info.getRecords());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, data);
	}

	@ApiOperation(value = "添加", notes = "")
	@PostMapping("/tenant")
	public Result<String> save(@RequestBody EditTenant entity,HttpServletRequest request){
		UserInfo loginInfo = UserInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		if(StringUtils.isEmpty(entity.getTenantId())) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "租户ID不能为空！！！", null);
		}
		Tenant target=new Tenant();
		BeanUtils.copyProperties(entity, target);
		QueryWrapper<Tenant> queryWrapper=new QueryWrapper<Tenant>();
		queryWrapper.select("tenant_id");
		queryWrapper.eq("tenant_id", entity.getTenantId());
		queryWrapper.orderByDesc("tenant_id");
		List<Tenant> list = tenantMapper.selectList(queryWrapper);
		if(list!=null&&list.size()>0) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "租户ID已存在！！！",null);
		}
		/*if(list!=null&&list.size()>0) {
			String tenantId = list.get(0).getTenantId();
			String createTenantId = createTenantId(tenantId);
			target.setTenantId(createTenantId);
		}else {
			target.setTenantId("00000000");
		}*/
		target.setCreateUser(loginInfo.getAccount());
		target.setUpdateTime(LocalDateTime.now());
		target.setCreateTime(LocalDateTime.now());	
		int rows = tenantMapper.insert(target);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}
	@ApiOperation(value = "修改", notes = "")
	@PutMapping("/tenant")
	public Result<String> edit(@RequestBody EditTenant entity,HttpServletRequest request){
		String ckTenantId = (String)redisUtil.get("edit:"+entity.getTenantId());
		if(StringUtils.isEmpty(ckTenantId)) {
			redisUtil.set("edit:"+entity.getTenantId(), entity.getTenantId(),60*10);
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "租户ID修改后影响很大，确认修改请重新提交！！！",null);
		}
		
		
		UserInfo loginInfo = UserInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		Tenant target=new Tenant();
		BeanUtils.copyProperties(entity, target);
		target.setUpdateUser(loginInfo.getAccount());
		target.setUpdateTime(LocalDateTime.now());
		int rows = tenantMapper.updateById(target);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}

	@ApiOperation(value = "删除", notes = "")
	@DeleteMapping("/tenant/{tenantId}")
	public Result<String> del(@PathVariable("tenantId") String tenantId){
		int rows = tenantMapper.deleteById(tenantId);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}
	
	@ApiOperation(value = "功能添加", notes = "")
	@PostMapping("/tenant/fun")
	public Result<String> setTenantFun(@RequestBody List<TenantFunction> tenantFunctions){
		for (TenantFunction tenantFunction : tenantFunctions) {
			tenantFunctionMapper.insert(tenantFunction);
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null , null);
	}
	public String createTenantId(String tenantId) {
		int i=0;
		for(;i<tenantId.split("").length;i++) {
			if(!tenantId.split("")[i].equals("0")) {
				break;
			}
		}
		String newId=(Integer.parseInt(tenantId.substring(i))+1)+"";
		int j=(8-newId.length());
		for(int k=0;k<j;k++) {
			newId="0"+newId;
		}
		return newId;
		
	}
}
